package com.sfh.cp.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfh.cp.R;

import crm.sfh.server.entity.EUser;

import com.sfh.cp.util.TimeUtil;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 用户
 */

public class WidgetUserTopLayout extends LinearLayout implements View.OnClickListener {



    CircleImageView ivHead;
    TextView tvTime;
    TextView tvName;
    ImageView ivSex;
    TextView tvAddress;
    ImageView ivDegree;
    TextView tvDegree;
    WidgetLabelLayout lbLyWealth;
    WidgetLabelLayout lbLyCharm;

    IContentOnClickLayout onClickUser;
    EUser user;

    public WidgetUserTopLayout(Context context) {
        super(context);
        init();
    }

    public WidgetUserTopLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WidgetUserTopLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        setGravity(Gravity.CENTER);
        this.setOrientation(HORIZONTAL);
        inflate(this.getContext(), R.layout.widget_user_top_layout, this);
        this.ivHead = this.find(R.id.ivHead);
        this.tvName = this.find(R.id.tvName);
        this.ivSex = this.find(R.id.ivSex);
        this.tvAddress = this.find(R.id.tvAddress);
        this.ivDegree = this.find(R.id.ivDegree);
        this.tvDegree = this.find(R.id.tvDegree);
        this.lbLyWealth = this.find(R.id.lbLyWealth);
        this.lbLyCharm = this.find(R.id.lbLyCharm);
        this.tvTime = this.find(R.id.tvTime);

        this.lbLyWealth.setStyleWealth("0");
        this.lbLyCharm.setStyleCharm("0");

        this.ivHead.setOnClickListener(this);
        this.tvName.setOnClickListener(this);

        this.lbLyWealth.setOnClickListener(this);
        this.lbLyCharm.setOnClickListener(this);
    }

    public void showData(EUser user) {

        this.user = user;
        if (user == null) {
            return;
        }
        this.tvName.setText(user.getName());
        this.ivSex.setImageResource(TextUtils.equals("1", user.getSex()) ? R.mipmap.tag_male : R.mipmap.tag_female);
        this.tvAddress.setText(user.getAddress());
        this.ivDegree.setImageResource(TextUtils.equals("1", user.getSex()) ? R.mipmap.tag_worker : R.mipmap.tag_school);
        this.tvDegree.setText(user.getDegree());

        if (user.getWealth() == 0L) {
            this.lbLyWealth.setVisibility(GONE);
        } else {
            this.lbLyWealth.setVisibility(VISIBLE);
            this.lbLyWealth.setStyleWealth(String.valueOf(user.getWealth()));
        }

        if (user.getCharm() == 0L) {
            this.lbLyCharm.setVisibility(GONE);
        } else {
            this.lbLyCharm.setVisibility(VISIBLE);
            this.lbLyCharm.setStyleCharm(String.valueOf(user.getCharm()));
        }


        if (TextUtils.isEmpty(user.getHead())) {
            this.ivHead.setImageResource(R.mipmap.userimg_default);
        } else {
            Picasso.with(getContext().getApplicationContext()).load(user.getHead()).into(this.ivHead);
        }
    }

    public void showTime(String time) {
        tvTime.setVisibility(VISIBLE);
        tvTime.setText(TimeUtil.friendly_time(time));
    }

    public void setOnClickUser(IContentOnClickLayout onClickUser) {
        this.onClickUser = onClickUser;
    }

    private <T> T find(int r) {
        return (T) findViewById(r);
    }

    @Override
    public void onClick(View v) {
        if (onClickUser == null) {
            return;
        }

        if (v == ivHead || v == tvName) {
            onClickUser.onClickUser(v, user);
            return;
        }
        if (v == lbLyWealth || v == lbLyCharm) {
            onClickUser.onWealthCharm(v, user);
            return;
        }
    }

    public TextView getTvTime() {
        return tvTime;
    }
}
